package com.vnp.cooking;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.vnp.cooking.bean.ObjCookpad;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class CommentFBActivity extends Activity implements OnClickListener{
	
	WebView webComment;
	ProgressBar progressBar;
	ImageButton btnBack, btnRefresh;
	
	ObjCookpad objCookpad = new ObjCookpad();
	
	String BASE_URL = "http://";
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_fb);
		
		objCookpad = getIntent().getParcelableExtra("objCookpad");
		
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnRefresh = (ImageButton) findViewById(R.id.btn_refresh);
		webComment = (WebView) findViewById(R.id.webComment);
		progressBar = (ProgressBar) findViewById(R.id.progressBar1);
		
		
		/*BufferedReader reader;
		String line;
		String html = "";
		try {
			reader = new BufferedReader(new FileReader("file:///assets/facebook_comment.html"));
			try {
				while((line = reader.readLine()) != null) {
				    html += line;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		webComment.loadDataWithBaseURL(BASE_URL, html, "text/html", null, null);*/
		loadComment();
		
		init();
	}
	
	
	void init() {
		btnBack.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);
	}
	
	void loadComment() {
		webComment.getSettings().setJavaScriptEnabled(true);
		webComment.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100) {
                	progressBar.setVisibility(ProgressBar.INVISIBLE);
                    progressBar.setProgress(0);
                } else {
                	progressBar.setVisibility(ProgressBar.VISIBLE);
                }
            }
        });
		
		webComment.loadUrl(BASE_URL+objCookpad.getUrlComment());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			closeActivity();
			break;
		case R.id.btn_refresh:
			webComment.stopLoading();
			webComment.clearHistory();
			loadComment();
			break;
		default:
			break;
		}
	}
	
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.open_main, R.anim.close_next);
	}


	@Override
	public void onBackPressed() {
		closeActivity();
	}


}
